package com.manager.plat.dao.impl;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.jdbc.core.simple.ParameterizedRowMapper;
import org.springframework.jdbc.core.support.JdbcDaoSupport;

import com.manager.plat.dao.IGatewayDao;
import com.manager.plat.util.BaseUtil;
import com.manager.plat.vo.GatewayVo;

public class GatewayDaoImpl extends JdbcDaoSupport implements IGatewayDao {
	private static Logger logger = Logger.getLogger(GatewayDaoImpl.class);
	
	/**获得所有有效的网关信息列表*/
	@SuppressWarnings("unchecked")
	@Override
	public List<GatewayVo> getGatewayList() {
		String sql = "select id, name, detail, sp_number, fee_type, fee_code, upper_id, mobile, long_number, linkid, minute, second, stime, etime, response from tb_ivr_gateway where status = 1";
		logger.info(String.format("获得所有有效的网关信息列表[%s]", sql));
		List<GatewayVo> gatewayVoList = this.getJdbcTemplate().query(sql, rowMapper);
		if(gatewayVoList != null && gatewayVoList.size() > 0){
			return gatewayVoList;
		}
		return null;
	}
	
	/**根据网关ID获得网关信息*/
	@SuppressWarnings("unchecked")
	@Override
	public GatewayVo getGatewayById(int id) {
		String sql = "select id, name, detail, sp_number, fee_type, fee_code, upper_id, mobile, long_number, linkid, minute, second, stime, etime, response from tb_ivr_gateway where id = ? and status = 1";
		Object[] param = new Object[]{id};
		logger.info(String.format("获得所有有效的网关信息列表[%s]", BaseUtil.logSQL(sql, param)));
		List<GatewayVo> gatewayVoList = this.getJdbcTemplate().query(sql, param, rowMapper);
		if(gatewayVoList != null && gatewayVoList.size() > 0){
			return gatewayVoList.get(0);
		}
		return null;
	}
	
	private ParameterizedRowMapper<GatewayVo> rowMapper = new ParameterizedRowMapper<GatewayVo>() {

		@Override
		public GatewayVo mapRow(ResultSet rs, int rowNum) throws SQLException {
			GatewayVo gatewayVo = new GatewayVo();
			gatewayVo.setId(rs.getInt("id"));
			gatewayVo.setName(rs.getString("name"));
			gatewayVo.setDetail(rs.getString("detail"));
			gatewayVo.setSp_number(rs.getString("sp_number"));
			gatewayVo.setFee_type(rs.getInt("fee_type"));
			gatewayVo.setFee_code(rs.getInt("fee_code"));
			gatewayVo.setUpper_id(rs.getInt("upper_id"));
			gatewayVo.setMobile(rs.getString("mobile"));
			gatewayVo.setLong_number(rs.getString("long_number"));
			gatewayVo.setLinkid(rs.getString("linkid"));
			gatewayVo.setMinute(rs.getString("minute"));
			gatewayVo.setSecond(rs.getString("second"));
			gatewayVo.setStime(rs.getString("stime"));
			gatewayVo.setEtime(rs.getString("etime"));
			gatewayVo.setResponse(rs.getString("response"));
			return gatewayVo;
		}
	};
	
	/**获得所有网关信息列表包括有效的和无效的*/
	@SuppressWarnings("unchecked")
	@Override
	public List<GatewayVo> getGatewayListAll() {
		
		String sql = "select id, sp_number from tb_ivr_gateway";
		logger.info(String.format("获得所有有效的网关信息列表[%s]", sql));
		List<GatewayVo> gatewayVoList = this.getJdbcTemplate().query(sql, tempMapper);
		if(gatewayVoList != null && gatewayVoList.size() > 0){
			return gatewayVoList;
		}
		return null;
	}
	private ParameterizedRowMapper<GatewayVo> tempMapper = new ParameterizedRowMapper<GatewayVo>() {

		@Override
		public GatewayVo mapRow(ResultSet rs, int rowNum) throws SQLException {
			GatewayVo gatewayVo = new GatewayVo();
			gatewayVo.setId(rs.getInt("id"));
			gatewayVo.setSp_number(rs.getString("sp_number"));
			return gatewayVo;
		}
	};
	/**保存网关信息*/
	@Override
	public int insertGateway(GatewayVo gatewayVo) {
		String sql = "insert tb_ivr_gateway(name, detail, sp_number, upper_id, fee_type, fee_code, mobile, long_number, linkid, minute, second, stime, etime, response, status, add_user) " +
					 "values(?,?,?,?,?,?,?,?,?,?,?,?,?,?,1,?)";
		Object[] param = new Object[]{gatewayVo.getName(), gatewayVo.getDetail(),
									gatewayVo.getSp_number(), gatewayVo.getUpper_id(),
									gatewayVo.getFee_type(), gatewayVo.getFee_code(),
									gatewayVo.getMobile(), gatewayVo.getLong_number(),
									gatewayVo.getLinkid(), gatewayVo.getMinute(),
									gatewayVo.getSecond(), gatewayVo.getStime(),
									gatewayVo.getEtime(), gatewayVo.getResponse(),
									gatewayVo.getAdd_user()};
		logger.debug(String.format("保存网关信息[%s]", BaseUtil.logSQL(sql, param)));
		
		int count = this.getJdbcTemplate().update(sql, param);
		return count;
	}
	/**更新网关信息*/
	@Override
	public int updateGateway(GatewayVo gatewayVo) {
		String sql = "update tb_ivr_gateway set name = ?, detail = ?, sp_number = ?, upper_id = ?, fee_type = ?, fee_code = ?, mobile = ?, long_number = ?, linkid = ?, minute = ?, second = ?, stime = ?, etime = ?, response = ?, update_user = ?, update_date = now() where id = ?";
	Object[] param = new Object[]{gatewayVo.getName(), gatewayVo.getDetail(),
								gatewayVo.getSp_number(), gatewayVo.getUpper_id(),
								gatewayVo.getFee_type(), gatewayVo.getFee_code(),
								gatewayVo.getMobile(), gatewayVo.getLong_number(),
								gatewayVo.getLinkid(), gatewayVo.getMinute(),
								gatewayVo.getSecond(), gatewayVo.getStime(),
								gatewayVo.getEtime(), gatewayVo.getResponse(),
								gatewayVo.getUpdate_user(), gatewayVo.getId()};
		logger.debug(String.format("更新网关信息[%s]", BaseUtil.logSQL(sql, param)));
		
		int count = this.getJdbcTemplate().update(sql, param);
		return count;
	}
	/**删除网关信息*/
	@Override
	public int deleteGateway(int id, int update_user) {
		String sql = "update tb_ivr_gateway set status = 0, update_user = ?, update_date = now() where id = ?";
		Object[] param = new Object[]{update_user, id};
		logger.debug(String.format("删除网关信息[%s]", BaseUtil.logSQL(sql, param)));
		
		int count = this.getJdbcTemplate().update(sql, param);
		return count;
	}
	
}
